package com.tiger.web.controller.communal;

import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.communal.domain.TenantUnit;
import com.tiger.communal.service.ITenantUnitService;
import com.tiger.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 【承租单位管理】Controller
 *
 * @author standard
 * @date 2022-09-21
 */
@Api(tags = "承租单位管理")
@RestController
@RequestMapping("/system/tenantUnit")
public class TenantUnitController extends BaseController {
    @Autowired
    private ITenantUnitService tenantUnitService;

    /**
     * 查询【承租单位管理】列表
     */
    @ApiOperation(value = "查询【承租单位管理】列表")
    //@PreAuthorize("@ss.hasPermi('system:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TenantUnit tenantUnit) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tenantUnit.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<TenantUnit> list = tenantUnitService.selectTenantUnitList(tenantUnit);
        return getDataTable(list);
    }


    /**
     * 获取【承租单位管理】详细信息
     */
    @ApiOperation(value = "获取【承租单位管理】列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tenantUnitService.selectTenantUnitById(id));
    }

    /**
     * 新增【承租单位管理】
     */
    @ApiOperation(value = "新增【承租单位管理】")
    //@PreAuthorize("@ss.hasPermi('system:unit:add')")
//    @Log(title = "【承租单位管理】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TenantUnit tenantUnit) throws IOException {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tenantUnit.setCompanyId(loginUser.getUser().getDeptId());
        return toAjax(tenantUnitService.insertTenantUnit(tenantUnit));
    }

    /**
     * 修改【承租单位管理】
     */
    @ApiOperation(value = "修改【承租单位管理】")
    //@PreAuthorize("@ss.hasPermi('system:unit:edit')")
//    @Log(title = "【承租单位管理】", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TenantUnit tenantUnit) {
        return toAjax(tenantUnitService.updateTenantUnit(tenantUnit));
    }

    /**
     * 删除【承租单位管理】
     */
    @ApiOperation(value = "删除【承租单位管理】")
    //@PreAuthorize("@ss.hasPermi('system:unit:remove')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tenantUnitService.deleteTenantUnitByIds(ids));
    }
}
